package day03_Socket;

import java.io.*;
import java.net.Socket;
import java.util.HashSet;


public class Server_Thread extends Thread{
    private Socket socket;
    public Server_Thread(Socket socket){
        this.socket = socket;
    }

    @Override
    public void run(){
        try {
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            String str = null;
            while (!((str = br.readLine()) == null)) {
                System.out.println(socket+"说:"+str);
                sendAll(socket+"说:"+str);
            }
        }
        catch (Exception e){
            System.out.println("失败");
            e.printStackTrace();
        }
    }

    public void sendAll(String str){
        BufferedWriter bw = null;
        for (Socket socket : Socket_server.hs) {
            try {
                bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                bw.write(str);
                bw.newLine();
                bw.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }


}
